<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class ForgotPassword extends CI_Controller
{
    public function index($page = 'login')
    {
        $this->load->library('form_validation');

        if (!$this->session->userdata('language')) {
            $this->session->set_userdata('language', 'en');
        }
        $this->lang->load($page, $this->session->userdata('language'));

        if (!file_exists('application/views/' . $page . '.php')) {
            show_404();
        }
        $data['title'] = ucfirst($page) . ' - ' . $this->session->userdata('language');
        $data['language'] = $this->session->userdata('language');

        $this->load->view('layout/header', $data);
        $this->load->view('login', $data);
        $this->load->view('layout/footer', $data);
    }

    public function doLogin()
    {
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'username', 'required');
        $this->form_validation->set_rules('password', 'password', 'required');
        if (!$this->form_validation->run()) {
            redirect(site_url('login'));
        } else {
            $username = $_POST['username'];
            $password = $_POST['password'];

            $query = $this->db->get_where('users', array('sLogin' => $username, 'sPwd' => $password));
            if ($query->num_rows() > 0) {
                redirect(site_url('dashboard'));
            } else {
                redirect(site_url('login'));
            }
        }
    }
}